package com.ggs.rg.ybjb.systemmodule.util;

import com.ggs.rg.ybjb.pojomodule.pojo.TUser;
import com.ggs.rg.ybjb.systemmodule.config.SpringUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.LogoutAware;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.crazycake.shiro.RedisSessionDAO;

import java.util.Collection;

/**
 * @Description Shiro工具类
 * @Author Sans
 * @CreateTime 2019/6/15 16:11
 */
public class ShiroUtils {

    /**
     * 私有构造器
     **/
    private ShiroUtils() {
    }


    private static RedisSessionDAO redisSessionDAO = SpringUtil.getBean(RedisSessionDAO.class);


    private static RedisUtil redisUtil = SpringUtil.getBean(RedisUtil.class);;

    /**
     * 获取当前用户Session
     *
     * @Author Sans
     * @CreateTime 2019/6/17 17:03
     * @Return SysUserEntity 用户信息
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * 用户登出
     *
     * @Author Sans
     * @CreateTime 2019/6/17 17:23
     */
    public static void logout() {
        SecurityUtils.getSubject().logout();
    }

    /**
     * 获取当前用户信息
     *
     * @Author Sans
     * @CreateTime 2019/6/17 17:03
     * @Return SysUserEntity 用户信息
     */
    public static TUser getUserInfo() {
        return (TUser) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * 删除用户在缓存中的sessionId信息,单点登录的核心
     *
     * @Author Sans
     * @CreateTime 2019/6/17 13:57
     * @Param username  用户名称
     * @Param isRemoveSession 是否删除Session
     * @Return void
     */
    public static void deleteCache(String phone, boolean isRemoveSession) {
        //从缓存中获取Session
        Session session = null;
        Collection<Session> sessions = redisSessionDAO.getActiveSessions();
        Object attribute = null;
        String sessionPhone = null;
        String s = null ;
        for (Session sessionInfo : sessions) {
            //遍历Session,找到该用户名称对应的Session
            attribute = sessionInfo.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            if (attribute == null) {
                continue;
            }
            sessionPhone = (String) ((SimplePrincipalCollection) attribute).getPrimaryPrincipal();
//            if (tUser == null) {
////                continue;
////            }
            if (phone.equals(sessionPhone) ) {
                System.out.println("同一个用户登录!!!");
                s = (String)sessionInfo.getId();
                System.out.println("ShiroUtils获取到的sessinId" + s);
                if (s.equals(SecurityUtils.getSubject().getSession().getId())){
                    continue;
                }
                System.out.println("执行到了这里");
                session = sessionInfo;
//                break;
            }
//            if (s != null)
//                break;
        }
        if (session == null || attribute == null) {
            System.out.println("session或attribute为空");
            return;
        }
        //删除session
        if (isRemoveSession) {
            System.out.println(session);
            System.out.println("开始删除原来的session");
//            System.out.println("要删除的key" + "shiro:session:"+s);
//            redisUtil.del("shiro:session:"+s);
            redisSessionDAO.delete(session);
        }
        //删除Cache，在访问受限接口时会重新授权
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        Authenticator authc = securityManager.getAuthenticator();
        ((LogoutAware) authc).onLogout((SimplePrincipalCollection) attribute);
    }
}

